{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "63215765",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数: 5\n",
      "空格个数: 2\n",
      "数字个数: 5\n",
      "其他字符个数: 0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 计算输入日期是该年的第几天\n",
    "def day_of_year(year, month, day):\n",
    "    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    if (year % 4 == 0 and year % 100!= 0) or year % 400 == 0:\n",
    "        days[1] = 29\n",
    "    total_days = sum(days[:month - 1]) + day\n",
    "    return total_days\n",
    "\n",
    "# 输出9x9乘法口诀表\n",
    "def multiplication_table():\n",
    "    for i in range(1, 10):\n",
    "        for j in range(1, i + 1):\n",
    "            print(f\"{j}x{i}={i * j}\", end=\"\\t\")\n",
    "        print()\n",
    "\n",
    "# 判断1000以内素数的个数并输出所有素数\n",
    "def count_primes():\n",
    "    primes = []\n",
    "    for num in range(2, 1001):\n",
    "        is_prime = True\n",
    "        for i in range(2, int(np.sqrt(num)) + 1):\n",
    "            if num % i == 0:\n",
    "                is_prime = False\n",
    "                break\n",
    "        if is_prime:\n",
    "            primes.append(num)\n",
    "    print(f\"1000以内素数的个数为: {len(primes)}\")\n",
    "    print(\"素数为:\", primes)\n",
    "\n",
    "# 对输入的正整数分解质因数\n",
    "def prime_factors(n):\n",
    "    i = 2\n",
    "    factors = []\n",
    "    while i * i <= n:\n",
    "        if n % i:\n",
    "            i += 1\n",
    "        else:\n",
    "            n //= i\n",
    "            factors.append(i)\n",
    "    if n > 1:\n",
    "        factors.append(n)\n",
    "    print(f\"{n} = {'*'.join(map(str, factors))}\")\n",
    "\n",
    "# 根据输入的成绩判断等级\n",
    "def grade_judgment():\n",
    "    score = int(input(\"请输入成绩: \"))\n",
    "    if score >= 90:\n",
    "        print(\"A\")\n",
    "    elif 60 <= score <= 89:\n",
    "        print(\"B\")\n",
    "    else:\n",
    "        print(\"C\")\n",
    "\n",
    "# 分析输入字符串中字母、空格、数字和其他字符的个数\n",
    "def digit_analysis():\n",
    "    string = input(\"请输入字符串: \")\n",
    "    letters = 0\n",
    "    spaces = 0\n",
    "    digits = 0\n",
    "    others = 0\n",
    "    for char in string:\n",
    "        if char.isalpha():\n",
    "            letters += 1\n",
    "        elif char.isspace():\n",
    "            spaces += 1\n",
    "        elif char.isdigit():\n",
    "            digits += 1\n",
    "        else:\n",
    "            others += 1\n",
    "    print(f\"英文字母个数: {letters}\")\n",
    "    print(f\"空格个数: {spaces}\")\n",
    "    print(f\"数字个数: {digits}\")\n",
    "    print(f\"其他字符个数: {others}\")\n",
    "\n",
    "# 计算特定形式数字序列的和\n",
    "def sum_sequence():\n",
    "    a = int(input(\"请输入数字a: \"))\n",
    "    n = int(input(\"请输入相加的项数n: \"))\n",
    "    total = 0\n",
    "    current = 0\n",
    "    for i in range(n):\n",
    "        current = current * 10 + a\n",
    "        total += current\n",
    "    print(f\"结果为: {total}\")\n",
    "\n",
    "# 找出1000以内的所有完数\n",
    "def perfect_numbers():\n",
    "    perfects = []\n",
    "    for num in range(1, 1001):\n",
    "        factors = [i for i in range(1, num) if num % i == 0]\n",
    "        if sum(factors) == num:\n",
    "            perfects.append(num)\n",
    "    print(\"1000以内的完数为:\", perfects)\n",
    "\n",
    "# 计算给定分数序列的前N项之和\n",
    "def fraction_sum():\n",
    "    N = int(input(\"请输入N: \"))\n",
    "    numerator = 2\n",
    "    denominator = 1\n",
    "    total = 0\n",
    "    for _ in range(N):\n",
    "        total += numerator / denominator\n",
    "        numerator, denominator = numerator + denominator, numerator\n",
    "    print(f\"前{N}项之和为: {total}\")\n",
    "\n",
    "# 计算1到N的阶乘之和\n",
    "def factorial_sum():\n",
    "    N = int(input(\"请输入N: \"))\n",
    "    total = 0\n",
    "    factorial = 1\n",
    "    for i in range(1, N + 1):\n",
    "        factorial *= i\n",
    "        total += factorial\n",
    "    print(f\"1到{N}的阶乘之和为: {total}\")\n",
    "\n",
    "# 找出满足特定条件的整数\n",
    "def find_number():\n",
    "    for i in range(1, 10000):\n",
    "        if np.sqrt(i + 100).is_integer() and np.sqrt(i + 268).is_integer():\n",
    "            print(i)\n",
    "\n",
    "# 根据输入的奇数N打印菱形图案\n",
    "def print_diamond(N):\n",
    "    for i in range(1, N + 1, 2):\n",
    "        print(\" \" * ((N - i) // 2) + \"*\" * i)\n",
    "    for i in range(N - 2, 0, -2):\n",
    "        print(\" \" * ((N - i) // 2) + \"*\" * i)\n",
    "\n",
    "# 计算兔子繁殖问题中每个月的兔子总数\n",
    "def rabbit_population():\n",
    "    months = int(input(\"请输入月数: \"))\n",
    "    population = [1, 1]\n",
    "    for i in range(2, months):\n",
    "        population.append(population[i - 1] + population[i - 2])\n",
    "    print(f\"{months}个月后的兔子总数为: {population[-1]}\")\n",
    "\n",
    "# 对输入的10个数进行排序\n",
    "def sort_numbers():\n",
    "    numbers = list(map(int, input(\"请输入10个数字，以空格分隔: \").split()))\n",
    "    numbers.sort()\n",
    "    print(\"排序后的结果:\", numbers)\n",
    "\n",
    "# 计算输入的3x3矩阵对角线元素之和\n",
    "def matrix_diagonal_sum():\n",
    "    matrix = []\n",
    "    for _ in range(3):\n",
    "        row = list(map(int, input(\"请输入矩阵的一行，以空格分隔: \").split()))\n",
    "        matrix.append(row)\n",
    "    diagonal_sum = matrix[0][0] + matrix[1][1] + matrix[2][2] + matrix[0][2] + matrix[2][0]\n",
    "    print(f\"矩阵对角线元素之和为: {diagonal_sum}\")\n",
    "\n",
    "# 将输入的数插入已排序的数组中\n",
    "def insert_sorted_array():\n",
    "    sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n",
    "    num = int(input(\"请输入要插入的数: \"))\n",
    "    for i in range(len(sorted_array)):\n",
    "        if num <= sorted_array[i]:\n",
    "            sorted_array.insert(i, num)\n",
    "            break\n",
    "    else:\n",
    "        sorted_array.append(num)\n",
    "    print(\"插入后的数组:\", sorted_array)\n",
    "\n",
    "# 将数组中的元素循环移动指定位置\n",
    "def shift_numbers():\n",
    "    numbers = list(map(int, input(\"请输入数组，以空格分隔: \").split()))\n",
    "    m = int(input(\"请输入移动的位置数: \"))\n",
    "    numbers = numbers[-m:] + numbers[:-m]\n",
    "    print(\"移动后的数组:\", numbers)\n",
    "\n",
    "# 计算报数游戏中最后留下的人的编号\n",
    "def last_remaining():\n",
    "    n = int(input(\"请输入人数: \"))\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)\n",
    "        people.pop(index)\n",
    "    print(f\"最后留下的是原来第{people[0]}号的那位\")\n",
    "\n",
    "# 根据输入的偶数或奇数计算特定分数序列的和\n",
    "def calculate_sum(n):\n",
    "    if n % 2 == 0:\n",
    "        total = 0\n",
    "        for i in range(2, n + 1, 2):\n",
    "            total += 1 / i\n",
    "        return total\n",
    "    else:\n",
    "        total = 0\n",
    "        for i in range(1, n + 1, 2):\n",
    "            total += 1 / i\n",
    "        return total\n",
    "    # 获取用户输入的题目编号\n",
    "question = input(\"请输入题目编号（1-20）: \")\n",
    "\n",
    "# 根据题目编号调用相应函数\n",
    "if question == \"1\":\n",
    "    year = int(input(\"请输入年份: \"))\n",
    "    month = int(input(\"请输入月份: \"))\n",
    "    day = int(input(\"请输入日期: \"))\n",
    "    print(day_of_year(year, month, day))\n",
    "elif question == \"2\":\n",
    "    multiplication_table()\n",
    "elif question == \"3\":\n",
    "    count_primes()\n",
    "elif question == \"4\":\n",
    "    n = int(input(\"请输入正整数: \"))\n",
    "    prime_factors(n)\n",
    "elif question == \"5\":\n",
    "    grade_judgment()\n",
    "elif question == \"6\":\n",
    "    digit_analysis()\n",
    "elif question == \"7\":\n",
    "    sum_sequence()\n",
    "elif question == \"8\":\n",
    "    perfect_numbers()\n",
    "elif question == \"9\":\n",
    "    fraction_sum()\n",
    "elif question == \"10\":\n",
    "    factorial_sum()\n",
    "elif question == \"11\":\n",
    "    find_number()\n",
    "elif question == \"12\":\n",
    "    N = int(input(\"请输入奇数N: \"))\n",
    "    print_diamond(N)\n",
    "elif question == \"13\":\n",
    "    rabbit_population()\n",
    "elif question == \"14\":\n",
    "    sort_numbers()\n",
    "elif question == \"15\":\n",
    "    matrix_diagonal_sum()\n",
    "elif question == \"16\":\n",
    "    insert_sorted_array()\n",
    "elif question == \"17\":\n",
    "    shift_numbers()\n",
    "elif question == \"18\":\n",
    "    last_remaining()\n",
    "elif question == \"19\":\n",
    "    n = int(input(\"请输入整数n: \"))\n",
    "    print(calculate_sum(n))\n",
    "else:\n",
    "    print(\"无效的题目编号\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
